'use strict';

module.exports = app => {
    class HomeService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        //主页数据
        async index() {
            /**
             * 请求后台接口
             */
            const result = await this.ctx.fetch(this.app.urls('index'), {
                method: 'get',
                dataType: 'json',
            });
            let data = result.data;
//          this.app.logger.info(data);

			if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        //主页作业模块
        async task() {
            const result = await this.ctx.fetch(this.app.urls('task'), {
                method: 'get',
                dataType: 'json',
            });
            let data = result.data;

            this.app.logger.info(data);

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }
            for(let i =0 ;i<data.data.length;i++){
                data.data[i]['createTime'] = data.data[i]['createTime'].substr(0,10);
            }
            this.app.logger.info('--------------------------**********************');
            this.app.logger.info(data);
            return data.data;
        }

        //更多咨询
        async newslist(){
            const result = await this.ctx.fetch(this.app.urls('newslist'), {
                method: 'get',
                dataType: 'json',
            });

            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }



        //资讯详情
        async eduDetail(params){
            /**
             * 请求后台接口
             */
            const result = await this.ctx.fetch(this.app.urls('eduDetail'), {
                method: 'get',
                dataType: 'json',
                data:params,
            });

            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }


            return data.data;
        }
    }

    return HomeService;
};
